﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace HYSoft.Web
{
    public partial class equipment_add66 : System.Web.UI.Page
    {
        string adminnoss = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            adminnoss = Request.QueryString["adminnos"];
            //判断登陆是否超时
            if (Session["uid"] == null || Session["uid"] == "")
            {
                Context.Response.Write("<script language=\"javascript\" type=\"text/javascript\">");
                Context.Response.Write("alert('对不起,你登录时间超时,请重新登录');top.location.href=\"index.html\";");
                Context.Response.Write("</script>");
                Context.Response.End();
            }
            string units1 = "";

            //nodetype_change();

            if (!IsPostBack)
            {
                // comm_data da = new comm_data();
                //DataTable dt = da.chektable("usr2monitor", "where [user_index]=" + Session["uid"] + "");
                string sql = " [user_index]=" + Session["uid"] + "";
                DataTable dt = new BLL.usr2monitor().GetList(sql).Tables[0];
                //绑定所属监测点
                DropDownList2.DataSource = dt.DefaultView;
                DropDownList2.DataTextField = "addr";
                DropDownList2.DataValueField = "index";
                DropDownList2.DataBind();
                dt.Clear();
                dt.Dispose();

                //传感器的绑定
                sql = " [index]!=0";
                DataTable dt1 = new BLL.sensor_node().GetList(sql).Tables[0];
                if (dt1.Rows.Count > 0)
                {
                    DropDownList3.DataSource = dt1.DefaultView;
                    DropDownList3.DataTextField = "name";
                    DropDownList3.DataValueField = "adminno";
                    DropDownList3.DataBind();
                   
                    DropDownList3.Items.FindByValue(adminnoss).Selected = true;
                    dt1.Clear();
                    dt1.Dispose();
                }
                sql = " [index]=" + Session["uid"] + "";
                DataTable dt3 = new BLL.user().GetList(sql).Tables[0];

                // comm_data da3 = new comm_data();
                // DataTable dt3 = da3.chektable("[user]", "where [index]=" + Session["uid"] + "");
                //所属单位
                units1 = dt3.Rows[0]["unit"].ToString();
                dt3.Clear();
                dt3.Dispose();
                //检测员绑定
                sql = " [unit]='" + units1 + "' and [type]=0 and [intvalue2]=1 ";
                DataTable dt2 = new BLL.user().GetList(sql).Tables[0];
                // comm_data da2 = new comm_data();
                //DataTable dt2 = da2.backdatatalbeselect("select * from [user] where [unit]='" + units1 + "' and [type]=0 and [intvalue2]=1 ");
                if (dt2.Rows.Count > 0)
                {
                    DropDownList12.DataSource = dt2.DefaultView;
                    DropDownList12.DataTextField = "name";
                    DropDownList12.DataValueField = "index";
                    DropDownList12.DataBind();
                    dt2.Clear();
                    dt2.Dispose();
                }
                //该单位下mac地址绑定
                mac_list();
            }
        }

        private void mac_list()
        {
            //该单位某监测点的mac地址绑定
            string sql = " [unit_index]=" + Session["uid"] + " and [level2]=0 and [adminno]= " + DropDownList3.SelectedValue + "";
            DataTable dt2 = new BLL.mac_value().GetList(sql).Tables[0];
            //comm_data da2 = new comm_data();
            // DataTable dt2 = da2.backdatatalbeselect("select * from [mac_value] where [unit_index]=" + Session["uid"] + " and [level2]=0 and [adminno]= " + DropDownList3.SelectedValue + "");
            if (dt2.Rows.Count > 0)
            {
                DropDownList13.DataSource = dt2.DefaultView;
                DropDownList13.DataTextField = "mac";
                DropDownList13.DataValueField = "mac";
                DropDownList13.DataBind();
                dt2.Clear();
                dt2.Dispose();
                //DropDownList13.SelectedValue = Request.QueryString["tounit"].ToString();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //comm_data da = new comm_data();
            int text20 = (int)(decimal.Parse(this.TextBox20.Value) * (decimal)100);
            int text21 = (int)(decimal.Parse(this.TextBox21.Value) * (decimal)100);
            int text1 = (int)(decimal.Parse(this.TextBox1.Value) * (decimal)100);
            int text5 = (int)(decimal.Parse(this.TextBox5.Value) * (decimal)100);
            int text7 = (int)(decimal.Parse(this.TextBox7.Value) * (decimal)100);
            int text9 = (int)(decimal.Parse(this.TextBox9.Value) * (decimal)100);
            int text11 = (int)(decimal.Parse(this.TextBox11.Value) * (decimal)100);
            int text13 = (int)(decimal.Parse(this.TextBox13.Value) * (decimal)100);
            int text14 = int.Parse(this.TextBox14.Value);
            int text3 = int.Parse(this.TextBox3.Value);
            int text6 = int.Parse(this.TextBox6.Value);
            int text8 = int.Parse(this.TextBox8.Value);
            int text10 = int.Parse(this.TextBox10.Value);
            int text12 = int.Parse(this.TextBox12.Value);

            string fileNamee1 = "";
            string fileNamee = FileUpload1.FileName.ToString();
            int imgflag = 1;
            if (fileNamee == "")
            {
                fileNamee = "Displacementmeter.jpg";
                fileNamee1 = "defaultimg/" + fileNamee;
                imgflag = 0;
            }
            else
            {
                fileNamee1 = "files/" + fileNamee;
            }

            string text16 = this.TextBox16.Value;
            string text17 = this.TextBox17.Value;
            string list2text = this.DropDownList2.SelectedValue;
            string list12text = this.DropDownList12.SelectedValue;
            string text4 = this.TextBox4.Value;

            string otherdefault = "'正常','正常','正常','正常','正常'";

            string typ3 = fileNamee.Substring(fileNamee.LastIndexOf(".") + 1);//上传文件的后缀名

            typ3 = typ3.ToLower();
            //comm_data da8 = new comm_data();
            string sql = " mac='" + DropDownList13.SelectedValue + "'";
            DataTable dt8 = new BLL.gprsNode().GetList(sql).Tables[0];
            //DataTable dt8 = da8.chektable("gprsnode", "where mac='" + DropDownList13.SelectedValue + "'");

            if (typ3 == "gif" || typ3 == "jpg" || typ3 == "png")
            {
                if (imgflag == 1)
                    FileUpload1.SaveAs(Server.MapPath("files/") + fileNamee);
                Model.gprsNode gprnode = new Model.gprsNode();
                gprnode.adminno = int.Parse(DropDownList3.SelectedValue);
                gprnode.tux = fileNamee1;
                gprnode.areano = int.Parse(list2text);
                gprnode.intvalue3 = 0;
                gprnode.mac = int.Parse(DropDownList13.SelectedValue);
                gprnode.simno = text16;
                gprnode.addr = text17;
                gprnode.intvalue1 = text20;
                gprnode.intvalue2 = text21;
                gprnode.gongdian = text4;
                gprnode.value1 = text1;
                gprnode.rate1 = text3;
                gprnode.value2 = text5;
                gprnode.rate2 = text6;
                gprnode.value3 = text7;
                gprnode.rate3 = text8;
                gprnode.value4 = text9;
                gprnode.rate4 = text10;
                gprnode.value5 = text11;
                gprnode.rate5 = text12;
                gprnode.value0 = text13;
                gprnode.wendu = "正常";
                gprnode.dianchi = "正常";
                gprnode.dianliang = "正常";
                gprnode.chuanganqi = "正常";
                gprnode.jingbao = "正常";
                gprnode.rate0 = text14;
                //添加该传感器
                new BLL.gprsNode().Add(gprnode);
                Model.node_to_user nodeuser = new Model.node_to_user();
                nodeuser.user_index = int.Parse(list12text);
                nodeuser.mac = int.Parse(DropDownList13.SelectedValue);
                new BLL.node_to_user().Add(nodeuser);
                Model.mac_value macvalue = new BLL.mac_value().GetModel(int.Parse(DropDownList13.SelectedValue));
                macvalue.level2 = 1;
                new BLL.mac_value().Update(macvalue);
                // da.insert_data("gprsNode", "adminno,tux,areano,intvalue3,mac,simno,addr,intvalue1,intvalue2,gongdian,value1,rate1,value2,rate2,value3,rate3,value4,rate4,value5,rate5,value0,wendu,dianchi,dianliang,chuanganqi,jingbao,rate0", "'" + 66 + "','" + fileNamee1 + "','" + list2text + "','" + 0 + "','" + DropDownList13.SelectedValue + "','" + text16 + "','" + text17 + "','" + text20 + "','" + text21 + "','" + text4 + "','" + text1 + "','" + text3 + "','" + text5 + "','" + text6 + "','" + text7 + "','" + text8 + "','" + text9 + "','" + text10 + "','" + text11 + "','" + text12 + "','" + text13 + "'," + otherdefault + ",'" + text14 + "'");
                // da.insert_data("node_to_user", "User_index,mac", "'" + list12text + "','" + DropDownList13.SelectedValue + "'");
                // da.updatatable("mac_value", "[level2]=1 where [mac]=" + DropDownList13.SelectedValue + "");


                Response.Write("<script>alert('传感器节点添加成功！请继续添加。');window.location = 'equipment_add" + adminnoss + ".aspx?adminnos="+adminnoss+"';</script>");
            }
            else
            {
                Response.Write("<script>alert('安装图片未选择！');history.back();</script>");
            }
            // }

        }

        protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
        {
            string nodetype = DropDownList3.SelectedValue.ToString();
            //转换到相应的页面
            Response.Write("<script>window.location = 'equipment_add" + nodetype + ".aspx?adminnos=" + nodetype + "';</script>");
        }
    }
}